Crate tupleops[][src]

Expand description

Utility library to work with tuples.

Features:

  • Test if all elements are Ok: all_ok()

    features = ["all-ok"], included by default

    assert_eq!(
        all_ok((good(1), good(2), good(3))),
        Ok((1, 2, 3)),
    );
    assert_eq!(
        all_ok((good(1), bad(2), good(3))),
        Err((Ok(1), Err(2), Ok(3)))
    );
  • Test if all elements are Some: all_some()

    features = ["all-some"], included by default

    assert_eq!(
        all_some((Some(1), Some(2), Some(3))),
        Ok((1, 2, 3))
    );
    assert_eq!(
        all_some((Some(1), Option::<()>::None, Some(3))),
        Err((Some(1), None, Some(3)))
    );
  • Prepend an element to a tuple: prepend()

    features = ["prepend"], included by default

    assert_eq!(prepend(1, (2, 3, 4)), (1, 2, 3, 4));
  • Append an element to a tuple: append()

    features = ["append"], included by default

    assert_eq!(append((1, 2, 3), 4), (1, 2, 3, 4));
  • Concatenate two tuples: concat_tuples()

    features = ["concat"], included by default

    assert_eq!(concat_tuples((1, 2), (3, 4, 5)), (1, 2, 3, 4, 5));
  • Concatenate multiple tuples: concat_many()

    features = ["concat-many"], included by default

    assert_eq!(concat_many(((), (1,), (2, 3,), (4, 5, 6))), (1, 2, 3, 4, 5, 6));
  • Turn a reference to a tuple to a tuple of references: ref_tuple()

    features = ["ref"], included by default

    assert_eq!(ref_tuple(&(1, 2, 3)), (&1, &2, &3));
  • Turn a reference to a mutable tuple to a tuple of mutable references: ref_mut_tuple()

    features = ["ref-mut"], included by default

    assert_eq!(ref_mut_tuple(&mut (1, 2, 3)), (&mut 1, &mut 2, &mut 3));
  • Extract the first element of a tuple: unprepend()

    features = ["unprepend"], included by default

    assert_eq!(unprepend((1, 2, 3, 4)), (1, (2, 3, 4)));
  • Extract the last element of a tuple: unappend()

    features = ["unappend"], included by default

    assert_eq!(unappend((1, 2, 3, 4)), ((1, 2, 3), 4));
  • Call a function with the tuple members as arguments: apply()

    features = ["apply"], included by default

    fn add3(a: u32, b: u32, c: u32) -> u32 { a + b + c }
    
    let tpl3 = (1, 2, 3);
    assert_eq!(apply(&add3, tpl3), 6);
  • Element-wise wrap the element of a tuple in Option: option_tuple()

    features = ["option"], included by default

    assert_eq!(option_tuple(Some((1, 2, 3))), (Some(1), Some(2), Some(3)));
  • Get the length of a tuple: length()

    features = ["length"], included by default

    assert_eq!(<(u8, u16, u32) as TupleLength>::LENGTH, 3);
  • Map a tuple: map_tuple()

    features = ["[map](map_tuple)"], not included by default, because it needs the unstable feature generic_associated_types (GAT).

    struct MyTupleEnum(usize);
    
    impl TupleMapper for MyTupleEnum {
        type MapElem<Type> = (usize, Type);
    
        fn map_elem<Elem>(&mut self, elem: Elem) -> Self::MapElem<Elem> {
            let index = self.0;
            self.0 += 1;
            (index, elem)
        }
    }
    
    assert_eq!(
        map_tuple(MyTupleEnum(1), ("hello", "world", "!")),
        ((1, "hello"), (2, "world"), (3, "!")),
    )

When used in libraries, you should probably use default-features = false, and only opt in to the features you actually need.

Supported tuple lengths:

By default the selected operations are implemented to tuples upto a length of 16 elements (features = ["default-len"]). You can specify a higher limit by using feature = ["X"], where X can be 8, 16, 32, 64, 96, 128, 160, 192, 224, or 256. A higher number includes all lower numbers.

Beware: features = ["256"] needs about 5 GB of RAM to compile the module, so only use it if you actually need it.

Traits

Tupletuple

A tuple.

A tuple that is usable with all_ok().

TupleAllSomeall-some

A tuple that is usable with all_some().

A tuple and an element that are usable with append().

A function and a tuple that are usable with apply().

Two tuples that are usable with concat_tuples().

TupleConcatManyconcat-many

A tuple of tuples that is usable with concat_many().

A tuple with a known length.

A TupleMapper and a tuple that are usable with map_tuple().

Helper trait to element-wise map a tuple.

A tuple that is usable with option_tuple().

An element and a tuple that are usable with prepend().

A reference to a tuple that is usable with ref_tuple().

A reference to a mutable tuple that is usable with ref_mut_tuple().

A tuple that is usable with unappend().

A tuple that is usable with unprepend().

Functions

all_okall-ok

Element-wise unwrap a tuple of Results if all elements are good. Return the input otherwise.

all_someall-some

Element-wise unwrap a tuple of Options if all elements are good. Return the input otherwise.

appendappend

Append an element to a tuple.

applyapply

Call a function with the tuples members as arguments.

concat_manyconcat-many

Concatenate two or more tuples.

Concatenate two tuples.

lengthlength and feature-const_fn_trait_bound

Return the length of a tuple.

Element-wise map a tuple with a mapper.

Element-wise wrap the element of a tuple in Option.

prependprepend

Prepend an elemenent to a tuple.

Turn a reference to a mutable tuple into a tuple of mutable references.

Turn a reference to a tuple into a tuple of references.

unappendunappend

Extract the last element of a tuple, and return a tuple of the initial tuple and the last element.

unprependunprepend

Extract the first element of a tuple, and return a tuple of the first element and the last elements of the tuple.

Type Definitions

AllOkall-ok

The type when a tuple of Results is element-wise unwrapped.

AllSomeall-some

The type when a tuple of Options is element-wise unwrapped.

Appendappend

The resulting type when an element is appended to an initial tuple.

Applyapply

The resulting type when F is called with Tpl’s elements.

ConcatManyconcat-many

The resulting type when two or more tuples are concatenated.

The resulting type when two tuples are concatenated.

Headunprepend

The type of the first element of the tuple.

Initunappend

The resulting tuple when the last element is removed from a tuple.

Lastunappend

The type of the last element of the tuple.

The type of a tuple when element-wise mapped with a mapper.

The resulting tuple when all elements are wrapped in Option.

Prependprepend

The resulting type when an element is prepended to a tuple.

The resulting type when every element of this reference to a mutable tuple is turned into a mutable reference.

The resulting type when every element of this reference to a tuple is turned into a reference.

Tailunprepend

The resulting tuple when the first element is removed from a tuple.